package com.rykj.util;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by 水寒 on 2016/7/18. 网络操作管理类
 */
public class HttpManage {
    public static final MediaType JSON = MediaType.parse("application/json;charset=utf-8");
    private static final String TAG = "HttpManage";
    static OkHttpClient httpClient;
    private static HttpManage mNetUtil;

    private HttpManage() {
        httpClient = new OkHttpClient();
    }

    /**
     * 获取NetUtil实例
     */
    public static HttpManage getInstance() {
        if (mNetUtil == null) {
            mNetUtil = new HttpManage();
        }
        return mNetUtil;
    }

    public String httpGet(String url) throws IOException {
        Request request = new Request.Builder().url(url).build();
        Response response = httpClient.newCall(request).execute();
        return response.body().string(); // 返回的是string 类型，json的mapper可以直接处理
    }

    public String httpPost(String url, String json) throws IOException {
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(url).post(requestBody).build();
        Response response = httpClient.newCall(request).execute();
        return response.body().string();
    }

}
